package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dto.DischargeApplyDTO;
import com.woniu.dto.InpatientDischargeAppDTO;
import com.woniu.entity.InpatientDischargeApp;
import com.woniu.entity.InpatientRecord;
import com.woniu.mapper.InpatientDischargeAppMapper;
import com.woniu.mapper.InpatientRecordMapper;
import com.woniu.service.InpatientDischargeAppService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.utils.UserContext;
import com.woniu.vo.InpatientDischargeAppVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class InpatientDischargeAppServiceImpl implements InpatientDischargeAppService {
    @Resource
    private InpatientDischargeAppMapper inpatientDischargeAppMapper;
    @Resource
    private InpatientRecordMapper recordMapper;
    @Override
    public ResponseUtil recordList(InpatientDischargeAppDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPageNum(),
                queryDTO.getPageSize());
        List<InpatientDischargeAppVO> list = inpatientDischargeAppMapper.recordList(queryDTO);
        PageInfo<InpatientDischargeAppVO> pageInfo = new PageInfo<>(list);
        return pageInfo.getSize() > 0 ?
                ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil addRecord(DischargeApplyDTO dto) throws ParseException {
        // 1. 获取当前登录的医生ID
//        Integer loggedInDoctorId = 1;
        Integer loggedInDoctorId = UserContext.ContextUserId();

        // 2. 查找病人当前住院在哪张床
        InpatientRecord activeRecord = recordMapper.findActiveRecordByPatientId(dto.getPatientId());
        if (activeRecord == null) {
//            throw new RuntimeException("操作失败：未找到该患者的有效住院记录。");
            return ResponseUtil.get(ResponseEnum.PATIENT_NOT_IN_HOSPITAL);
        }

        // 3. 创建出院申请对象
        InpatientDischargeApp application = new InpatientDischargeApp();
        application.setOaCaseId(dto.getPatientId());
        application.setOaWorkerId(loggedInDoctorId);
        application.setOaDate(new Date());
        // 4. 设置状态 ("申请中 0")
        application.setOaStatus(0);
        // 5. 设置病床ID
        application.setOaBedId(activeRecord.getHzBedId());
        // 6. 更新住院表，将状态改为1：已申请出院
        activeRecord.setHzStatus(1);
        // 7. 更新住院表
        Integer recordUpdate = recordMapper.updateByPrimaryKeySelective(activeRecord);
        // 6. 插入数据库
        Integer result = inpatientDischargeAppMapper.insertSelective(application);

        return result > 0 && recordUpdate > 0 ?
                ResponseUtil.get(ResponseEnum.OK, application.getId())
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil updateRecord(InpatientDischargeApp record) {
        Integer result = inpatientDischargeAppMapper
                .updateByPrimaryKeySelective(record);
        return result > 0 ?
                ResponseUtil.get(ResponseEnum.OK,record)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
}
